package osb.map.main;

import osb.map.database.ConfigurationRW;
import osb.map.net.ClientSocket;
import osb.map.net.DataPackage;
import osb.map.net.DataUser;
import osb.map.net.msgType;
import osb.map.publicdata.Public_Operate_And_Data;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.ljp.ani.R;

/**
 * 登录成功 - 账户信息
 * @author Administrator
 *
 */
public class Login_Accessed extends Activity
{
    static public Login_Accessed Public_Static_Login_Accessed;
    // 按钮状态  [true]注册      [false]修改密码
    public boolean Button_State = true;
    Button button_exit;
    Button button_changepassword;
    EditText frist_1;
    EditText frist_2;
    EditText second_1;
    EditText second_2;
    EditText third_1;
    EditText third_2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_accessed);
        Public_Static_Login_Accessed = this;

        button_exit = (Button) findViewById(R.id.exit); // 退出账号
        button_changepassword = (Button) findViewById(R.id.changepassword); // 修改密码
        frist_1 = (EditText) findViewById(R.id.frist_1);   // 用户名
        frist_2 = (EditText) findViewById(R.id.frist_2);   // 用户名
        second_1 = (EditText) findViewById(R.id.second_1); // 账号
        second_2 = (EditText) findViewById(R.id.second_2); // 账号
        third_1 = (EditText) findViewById(R.id.third_1);   // 余额
        third_2 = (EditText) findViewById(R.id.third_2);   // 余额
        
        // 初始化为正常模式
        State_TO_Frist();
        
        // 退出登录
        button_exit.setOnClickListener(new OnClickListener()  
        {
            @Override
            public void onClick(View v)
            {
                if(Button_State)
                {
                    if(Public_Operate_And_Data.IsAutoLogin)
                    {
                        ConfigurationRW.Configuration_W_this("false", Public_Operate_And_Data.User_id, "" + Public_Operate_And_Data.UI_Stype_INT_Curren, Public_Operate_And_Data.User_name, "" + Public_Operate_And_Data.User_accout, Login_Accessed.Public_Static_Login_Accessed); //将用户ID写入配置文件
                    }
                    Public_Operate_And_Data.Islogin = false;  
                    Public_Operate_And_Data.IsAutoLogin = false; 
                    Intent intent = new Intent();
                    intent.setClass(Login_Accessed.this, Login_Main.class);  
                    startActivity(intent);
                    Login_Accessed.this.finish();  
                }
                else
                {
                    State_TO_Frist();
                }
            }
        });
        // 修改密码
        button_changepassword.setOnClickListener(new OnClickListener()  
        {
            @Override
            public void onClick(View v)
            {
                if(Button_State)
                {
                    State_TO_Second();
                }
                else 
                {
                    String password_firString = frist_2.getText().toString();
                    String password_second = second_2.getText().toString();
                    String password_third = third_2.getText().toString();
                    if (password_firString.equals(""))
                    {
                        Toast.makeText(Login_Accessed.this, "请输入原密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (password_second.equals(""))
                    {
                        Toast.makeText(Login_Accessed.this, "请输入新密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (password_third.equals(""))
                    {
                        Toast.makeText(Login_Accessed.this, "请输入确认密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(!password_second.equals(password_third))
                    {
                        Toast.makeText(Login_Accessed.this, "输入的新密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(!password_firString.equals(Public_Operate_And_Data.user_pwd))
                    {
                        Toast.makeText(Login_Accessed.this, "原密码错误",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Public_Operate_And_Data.user_pwd_willchange = password_third; 

                    // 服务器验证
                    DataPackage data = new DataPackage(msgType.ModPwd_User); 
                    data.dataUser = new DataUser(Public_Operate_And_Data.User_id, password_third, null);
                    ClientSocket.Public_Static_ClientSocket.sendMsg(data);
                }
            }
        });
    }
    
    /**
     * 修改密码成功
     */
    public void ModpwdSuccess()
    {
        new AlertDialog.Builder(Login_Accessed.this)
        .setMessage("修改成功，请牢记您的密码")
        .setPositiveButton("确定", null)
        .show();
        State_TO_Frist();
    }
    
    /**
     * 修改密码失败
     */
    public void ModpwdWrong() 
    {
        new AlertDialog.Builder(this)
        .setMessage("修改失败，请重新修改")
        .setPositiveButton("确定", null)
        .show();
    }
    
    /**
     * 进入密码修改模式
     */
    public void State_TO_Second() 
    {
        frist_1.setText("原密码:");
        frist_2.setEnabled(true);
        frist_2.setText("");
        frist_2.setInputType(0x81); 

        second_1.setText("新密码:");
        second_2.setEnabled(true);
        second_2.setText("");
        second_2.setInputType(0x81);

        third_1.setText("确认密码:");
        third_2.setEnabled(true);
        third_2.setText("");
        third_2.setInputType(0x81);

        button_exit.setText(" 取消 ");
        button_changepassword.setText("确认");
        Button_State = false; 
    }
    
    /**
     * 进入正常模式
     */
    public void State_TO_Frist()  
    {
        String id = Public_Operate_And_Data.User_id;
        String name = Public_Operate_And_Data.User_name;
        String account = Public_Operate_And_Data.User_accout + "";
        frist_1.setText("姓名:");
        frist_2.setText(name);
        frist_2.setInputType(0x90);
        frist_2.setEnabled(false);

        second_1.setText("账号:");
        second_2.setText(id);
        second_2.setInputType(0x90);
        second_2.setEnabled(false);

        third_1.setText("余额:");
        third_2.setText(account);
        third_2.setInputType(0x90);
        third_2.setEnabled(false);
        button_exit.setText(" 退出账号 ");
        button_changepassword.setText("修改密码");
        Button_State = true; 
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (Button_State)   
            {
                finish();
            }
            else   
            {
                State_TO_Frist();

            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    /**
	 * menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 3, "分享下载").setIcon(android.R.drawable.ic_menu_send);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent intent = new Intent();
			intent.setClass(Login_Accessed.this, ShareActivity.class);
			Login_Accessed.this.startActivity(intent);

			break;
		}
		return false;
	}
}